package com.seocoo.worker.user;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.seocoo.worker.R;
import com.seocoo.worker.user.login.entity.EAdept;

import java.util.ArrayList;
import java.util.List;

/***
 * 选择擅长领域
 */
public class AdeptDialog extends PopupWindow implements View.OnClickListener {


    public interface IOnItemClick {
        void onSelectValue(AdeptDialog dialog, List<EAdept> list);
    }

    Context mContext;
    IOnItemClick onItemClick;
    SelectAdapter adapter;

    public AdeptDialog(Context context, List<EAdept> data,String[] selectAdep, IOnItemClick onItemClick) {

        super(context);
        this.mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.adept_select_dialog, null, false);
        setContentView(view);

        ColorDrawable background = new ColorDrawable();
        background.setAlpha(100);
        this.setBackgroundDrawable(background);
        this.setFocusable(true);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setOutsideTouchable(true);


        this.onItemClick = onItemClick;

        view.findViewById(R.id.ivColse).setOnClickListener(this);
        view.findViewById(R.id.tv_cancle).setOnClickListener(this);
        view.findViewById(R.id.tv_ok).setOnClickListener(this);

        ListView lvContent = view.findViewById(R.id.lvContent);
        this.adapter = new SelectAdapter(context, data,selectAdep);
        lvContent.setAdapter(this.adapter);

    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_cancle || v.getId() == R.id.ivColse) {
            dismiss();
        } else if (v.getId()  == R.id.tv_ok){
            onItemClick.onSelectValue(this, adapter.selectData);
        }
    }

    class SelectAdapter extends BaseAdapter {

        List<EAdept> selectData = new ArrayList<>();
        List<EAdept> mData;

        LayoutInflater layoutInflater;

        Context context;

        public SelectAdapter(Context context, List<EAdept> mData,String[] selectAdep) {

            super();
            this.context = context;
            this.mData = mData;
            this.layoutInflater = LayoutInflater.from(context);
            if (selectAdep != null && selectAdep.length > 0){
                for (String classifyId: selectAdep){
                    for (EAdept adept:mData){
                        if (TextUtils.equals(classifyId,adept.classifyId)){
                            this.selectData.add(adept);
                            break;
                        }
                    }
                }
            }
        }

        @Override
        public int getCount() {

            return mData.size();
        }

        @Override
        public EAdept getItem(int position) {

            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ItemView itemView = null;
            if (convertView == null) {
                itemView = new ItemView();
                convertView = layoutInflater.inflate(R.layout.adept_select_adapter, parent, false);
                itemView.cb_select = convertView.findViewById(R.id.cb_select);
                itemView.tv_value = convertView.findViewById(R.id.tv_value);

                convertView.setTag(itemView);
            } else {
                itemView = (ItemView) convertView.getTag();
            }

            final EAdept bank = getItem(position);
            itemView.tv_value.setText(bank.classifyName);

            if (selectData.contains(bank)) {
                itemView.cb_select.setChecked(true);
            } else {

                itemView.cb_select.setChecked(false);

            }
            itemView.tv_value.setOnClickListener(v -> {
                if (selectData.contains(bank)) {
                    selectData.remove(bank);
                } else {
                    selectData.add(bank);
                }
                notifyDataSetChanged();
            });
            itemView.cb_select.setOnClickListener(v -> {
                if (selectData.contains(bank)) {
                    selectData.remove(bank);
                } else {
                    selectData.add(bank);
                }
                notifyDataSetChanged();
            });
            return convertView;
        }

        class ItemView {

            CheckBox cb_select;

            TextView tv_value;
        }


    }

    public void show(View parent) {

        if (isShowing()) {
            return;
        }
        super.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }
}
